<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 公共辅助函数
 */

/**
 * 获取广告数据
 *
 * @param string $ckey 广告位标识符
 * @return array array($advert_html, $advert_array); [0]当前显示的HTML广告内容 [1]多广告内容数组
 */
function get_advert($ckey) {
    $CI = &get_instance();

    $CI->load->driver('cache');
    if (!$cache = $CI->cache->file->get('advert')) {
        $CI->load->service('cache/cache_update_service');
        $cache = $CI->cache_update_service->update_advert();
    }

    if (empty($cache[$ckey])) return false;
    $tmp_advert = $cache[$ckey];
    if ($cache['config'][$ckey] == 'rand') shuffle($tmp_advert);

    $advert_html = '';
    $advert_array = array();
    foreach ($tmp_advert as $key => $value) {
        //由于缓存的原因，可能时间已经过期，需要再次判断一次
        if ($value['stime'] > TIMESTAMP || $value['etime'] < TIMESTAMP) {
            continue;
        }
        $advert_array[] = $value['code'];
        $advert_html .= is_array($value['code']) ? $value['code']['code'] : $value['code'];
        if ($cache['config'][$ckey] != 'all') break;
    }

    return array($advert_html, $advert_array);
}


/* End of file common_helper.php */
/* Location: ./application/helpers/common_helper.php */
